package com.sbw.java.advanced.demo.config;

import com.sbw.java.advanced.demo.remote.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author sangbowei
 * @date 2018年7月9日
 *  配置httpclient
 */
@Configuration
@Import(HttpClient.class)
public class HttpClientConfig {
	
	@Value("${http.maxTotal}")
    private Integer maxTotal;
 
    @Value("${http.defaultMaxPerRoute}")
    private Integer defaultMaxPerRoute;
 
    @Value("${http.connectTimeout}")
    private Integer connectTimeout;
 
    @Value("${http.connectionRequestTimeout}")
    private Integer connectionRequestTimeout;
 
    @Value("${http.socketTimeout}")
    private Integer socketTimeout;
 
    @Value("${http.staleConnectionCheckEnabled}")
    private boolean staleConnectionCheckEnabled;
 
    //定义httpclient连接池
    @Bean(name = "httpClientConnectionManager")
    public PoolingHttpClientConnectionManager getHttpClientConnectionManager(){
        PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
        //设置连接总数
        httpClientConnectionManager.setMaxTotal(maxTotal);
        //设置每个地址的并发数
        httpClientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
        return httpClientConnectionManager;
    }
 
    //定义 HttpClient工厂,这里使用HttpClientBuilder构建
    @Bean(name = "httpClientBuilder")
    public HttpClientBuilder getHttpClientBuilder(@Qualifier("httpClientConnectionManager")PoolingHttpClientConnectionManager httpClientConnectionManager){
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setConnectionManager(httpClientConnectionManager);
        return httpClientBuilder;
    }
 
    @Bean
    public CloseableHttpClient getCloseableHttpClient(@Qualifier("httpClientBuilder") HttpClientBuilder httpClientBuilder){
    	return httpClientBuilder.build();
    }
 
    //定义requestConfig的工厂
    @Bean(name = "builder")
    public RequestConfig.Builder getBuilder(){
        RequestConfig.Builder builder = RequestConfig.custom();
        //创建连接的最长时间
        builder.setConnectTimeout(connectTimeout)
		       //从连接池中获取到连接的最长时间
        	   .setConnectionRequestTimeout(connectionRequestTimeout)
        	   //数据传输的最长时间
        	   .setSocketTimeout(socketTimeout)
        	   //提交请求前测试连接是否可用
        	   .setStaleConnectionCheckEnabled(staleConnectionCheckEnabled);
        return builder;
    }

    //得到requestConfig实例
    @Bean
    public RequestConfig getRequestConfig(@Qualifier("builder") RequestConfig.Builder builder){
        return builder.build();
    }

}
